import { http } from '@/plugins/axios'
import { reactive, ref } from 'vue'

export default () => {
    enum Method {
        get = 'config_get',
        update = 'config_update'
    }

    const handleMethod = (method: Method) => {
        let func: Function = () => {
        }
        switch (method) {
            case Method.get:
                func = () => config_get()
                break
            case Method.update:
                func = (id: number, data: any) => config_update(data)
                break
        }
        return func
    }

    const tabs = ref([
        { label: '网站配置', route: { name: 'shop.admin.config.edit' } },
    ])
    //定义ConfigModel类型的configData
    let configData = reactive({
        //需要的form字段
        var: {
            title: '',
            keywords: '',
            tel: '',
            description: '',
            apis: {
                mch_id: '',//收款商户号ID
                mch_secret_key: '',//商户号秘钥
                mini_app_id: '',//小程序APPID
                notify_url: '',//支付通知地址
            },
        },
        //对应的正则验证规则
        rules: {
            title: [
                {
                    required: true,
                    message: '请输入标题',
                },
            ],
            keywords: [
                {
                    required: true,
                    message: '请输入关键词',
                },
            ],
            tel: [
                {
                    required: true,
                    message: '请输入联系方式',
                },
            ],
            description: [
                {
                    required: true,
                    message: '请填写网站介绍',
                },
            ],
            'goods.thumb_width': [
                {
                    required: true,
                    message: '商品宽度',
                },
            ],
            'goods.thumb_height': [
                {
                    required: true,
                    message: '商品高度',
                },
            ],
            'apis.mch_id': [
                {
                    required: true,
                    message: '收款商户号ID',
                },
            ],
            'apis.mch_secret_key': [
                {
                    required: true,
                    message: '商户号秘钥',
                },
            ],
            'apis.mini_app_id': [
                {
                    required: true,
                    message: '小程序APPID',
                },
            ],
            'apis.notify_url': [
                {
                    required: true,
                    message: '支付通知地址',
                },
            ],
        },
        handle: handleMethod,
    })

    //所有评论
    function config_get() {
        return http.request<ApiPage<ConfigModel>[]>({
            url: `config`,
            method: 'GET',
        })
    }

    //   商品评论
    function config_update(data: any) {
        return http.request<ApiPage<ConfigModel>>({
            url: `config`,
            method: 'PUT',
            data: data,
        })
    }

    //首次运行一下
    const fillData = async () => {
        // @ts-ignore
        configData.var = await config_get()
    }
    // fillData()
    return {
        tabs,
        configData,
        fillData,
        config_update,
    }
}
